import axios from "axios";
import qs from 'qs';
import {
  Message
} from "element-ui";

const config = {
  baseURL: process.env.VUE_APP_BASE_URL,

  login: "/jwt/auth", //登陆接口
  queryAdvancedPolicy: "/policy/queryAdvancedPolicy", //报案保单查询
  yiYongCaseRegistCheck: "/regist/yiYongCaseRegistCheck", //点击报案
  isRegist: "/regist/isRegist", //是否重复报案
  submitRegist: "/regist/submitRegist", //报案提交
  yiYongCaseRegistCheck: "regist/yiYongCaseRegistCheck", //报案登记检查
  initRegist: "regist/initRegist", //初始化报案页面
  queryTaskByUser: "bpm/queryTaskByUser", //任务中心--任务查询(综合查询中任务查询也用这个接口)
  queryAdvancedCase: "caseSearch/queryAdvancedCase", //案件查询



  loginInfo: "/saa/login_info",
  companyQuery: "/syscompany/listsub/{comCode}",
  userQuery: "/sysuser/list",
  userCreate: "/sysuser/create",
  userEdit: "/sysuser/update",
  userDelete: "/sysuser/delete",
  // saa 角色相关
  roleListAll: "/saa/role/list?_pageNo={_pageNo}&_pageSize={_pageSize}",
  taskTreeWithUser: "/saa/task/tree/{userCode}",
  createRole: "/saa/role/create",
  deleteRole: "/saa/role/delete",
  updateRole: "/saa/role/update",
  outsideRoleCode: "/saa/outside/{roleCode}", // 查找不在某角色下的所有用户
  deleteUsersFromRole: "/saa/delete/{roleCode}",
  existingUserQuery: "/saa/user-grants/{roleCode}", //在指定的角色下已经添加的用户
  addUserToRole: "/saa/adduser/{roleCode}", //在指定的角色下添加用户
  // saa 机构相关
  getSubCompany: "/syscompany/sub/{comCode}",
  createOrganization: "/syscompany/create",
  deleteOrganization: "/syscompany/delete",
  updateOrganization: "/syscompany/update",
  availableOrganization: "/syscompany/available", // 根据用户userCode查看有效的机构
  dragOrganization: "/syscompany/drag",
  // saa 菜单相关
  getMenuWithUser: "/saa/task/tree/{userCode}", //根据用户userCode查看菜单信息
  getSubMenu: "/saa/task/sub/{taskCode}", // 获取菜单的子节点
  deleteMenu: "/saa/task/delete",
  createMenu: "/saa/task/create",
  updateMenu: "/saa/task/update"
};
// 公共信息的配置
axios.defaults.baseURL = process.env.VUE_APP_BASE_URL; // 根据环境变量设置默认请求后台地址
axios.config = config;
axios.defaults.timeout = 20000; // 请求超时时间

/**
 * 请求拦截器
 */
axios.interceptors.request.use(
  config => {
    debugger
    // 在请求发送之前做一些处理，让每个请求携带JWT token-- ['Authorization'] 请根据实际情况自行修改
    config.data = config.data ? config.data : [];
    config.data["Authorization"] = localStorage.getItem("loginData") ? JSON.parse(localStorage.getItem("loginData")).token : "";
    if (config.method === "post") {
      debugger
      config.data = qs.stringify({
        ...config.data
      })
    }
    return config;
  },
  error => {
    // 发送失败
    console.log(error);
    Promise.reject(error);
  }
);

/**
 * 响应拦截器
 */
axios.interceptors.response.use(
  response => {
    // 这个“status状态码”和“statusText状态信息”是和后端约定的，需前后端严格按照规范来处理
    if (response.data && response.data.status !== 0) {
      Message({
        message: response.data.statusText ? response.data.statusText : '请求后台服务出错，请重试或者联系管理员！',
        type: "error",
        duration: 5000
      });
    } else {
      return response.data
    }
  },

  error => {
    // 后台请求错误处理，这里可以埋点
    if (error && error.response) {
      switch (error.response.status) {
        case 400:
          error.message = "请求错误";
          break;
        case 401:
          error.message = "未授权，请登录";
          break;
        case 403:
          error.message = "拒绝访问";
          break;
        case 404:
          error.message = `请求地址出错: ${error.response.config.url}`;
          break;
        case 408:
          error.message = "请求超时";
          break;
        case 500:
          error.message = "服务器内部错误";
          break;
        case 501:
          error.message = "服务未实现";
          break;
        case 502:
          error.message = "网关错误";
          break;
        case 503:
          error.message = "服务不可用";
          break;
        case 504:
          error.message = "网关超时";
          break;
        case 505:
          error.message = "HTTP版本不受支持";
          break;
        default:
          break;
      }
    }
    Message({
      message: error.message,
      type: "error",
      duration: 5000
    });
    return Promise.reject(error);
  }
);

export default axios;
export {
  config
}; // 后台接口配置信息导出，方便配置Mock使用
